<?php


namespace app\api\model;


use think\Model;

class File extends Model
{
    protected $autoWriteTimestamp = true;

    protected $dateFormat = 'Y-m-d H:i:s';

    protected $type = [
        'upload_time' => 'timestamp',
    ];

    protected static function init()
    {
        // 判断该资源是否已上传,若上传则不再记录
        self::beforeInsert(function ($model) {
            if (self::where('uri', '=', $model['uri'])->find()) {
                return false;
            }
        });
        self::beforeWrite(function ($row) {
            if (isset($row['tid']) && $row['tid'] == '') {
                $row['tid'] = 0;
            }
        });
    }
}